package com.bawei.monthdemo2207a.model.http

import android.app.DownloadManager
import android.app.DownloadManager.Request
import android.content.Context
import android.net.Uri
import android.os.Environment

/**
 * 作者：赵亮
 * 时间：2024/12/26 17:09
 * 功能：
 */
object DownloadUtil {

    fun download(context: Context, url:String, name:String) {
        // 1. 创建下载请求
        val request = Request(Uri.parse(url))

        // 2. 设置下载地址
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, name)

        // 3. 显示通知
        request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
        request.setTitle(name)
        request.setDescription("下载中")

        // 4. 执行下载
        val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        downloadManager.enqueue(request)
    }
}